//
//  GYStorageUserViewModel.m
//  GYDemo
//
//  Created by Mrzhang on 2018/5/16.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "GYStorageUserViewModel.h"
#import "Head.h"
//@implementation GYTableSectionViewModel
//-(instancetype)initWithTitle:(NSString *)title items:(NSArray *)items
//{
//    if (self = [super init]) {
//        _sectionTitle = title;
//        NSMutableArray *marr = [[NSMutableArray alloc]initWithArray:items];
//        _items = marr;
//    }
//    return self;
//}
//@end


@implementation GYStorageUserInfoModel
@end


@implementation GYStorageUserModel
@end

@interface GYStorageUserViewModel()
@end

@implementation GYStorageUserViewModel
#pragma mark - getter and setter
-(NSString *)userPhone
{
    return self.storageUserInfoModel.mobile;
}
-(NSString *)userShopLevel
{
    return self.storageUserInfoModel.xlevel;
}
-(NSString *)userCompanyAuthoStatus
{
    if (!_userCompanyAuthoStatus) {
        _userCompanyAuthoStatus = @"1";
    }
    return _userCompanyAuthoStatus;
}
-(NSMutableArray *)userDetails
{
        NSMutableArray *details = [NSMutableArray new];
        NSInteger ltype=[[GYLoginViewModel shareInterface] roleType];
//,@"LogisticsAuth",@"StorageAuth"
        GYTableSectionViewModel *section1 = [[GYTableSectionViewModel alloc]initWithTitle:@"账户管理" items:@[
                                                                                                          [[GYUserImageTitleCellModel alloc]initWithImage:@"hyzx_zhxx" andTitle:@"账户信息" andAction:@"accountInfo"],                                                                                                          [[GYUserImageTitleCellModel alloc]initWithImage:@"hyzx_qytzxx" andTitle:@"企业认证信息" andAction:ltype==2?@"StorageAuth":@"LogisticsAuth"],
                                                                                                          [[GYUserImageTitleCellModel alloc]initWithImage:@"hyzx_xgmm" andTitle:@"修改密码" andAction:@"changePassword"]
                                                                                                          ]];

        GYTableSectionViewModel *section4 = [[GYTableSectionViewModel alloc]initWithTitle:ltype==2?@"革优仓储":@"革优物流" items:ltype==2?@[
                                                                                                                           [[GYUserImageTitleCellModel alloc]initWithImage:@"hyzx_dpxx" andTitle:@"店铺编辑" andAction:@"LogisticShopInfo"],
                                                                                                                           [[GYUserImageTitleCellModel alloc]initWithImage:@"hyzx_ghjl" andTitle:@"服务信息" andAction:@"sotrageServerService"],
                                                                                                                           [[GYUserImageTitleCellModel alloc]initWithImage:@"hyzx_xsdd" andTitle:@"订单管理" andAction:@"sotrageServerOrder"]
                                                                                                                           ]:@[
                                                                                                                                                                                                                                                    [[GYUserImageTitleCellModel alloc]initWithImage:@"hyzx_dpxx" andTitle:@"店铺信息" andAction:@"LogisticShopInfo"],
                                                                                                                                                                                                                                                    [[GYUserImageTitleCellModel alloc]initWithImage:@"hyzx_xsdd" andTitle:@"服务列表" andAction:@"logisticsServerService"],
                                                                                                                                                                                                                                                    [[GYUserImageTitleCellModel alloc]initWithImage:@"hyzx_xsdd" andTitle:@"报价列表" andAction:@"logisticsServerOffer"],
                                                                                                                                                                                                                                                    [[GYUserImageTitleCellModel alloc]initWithImage:@"hyzx_ghjl" andTitle:@"订单管理" andAction:@"logisticsServerOrder"]]];

        GYTableSectionViewModel *section6 = [[GYTableSectionViewModel alloc]initWithTitle:@"客户服务" items:@[
                                                                                                          [[GYUserImageTitleCellModel alloc]initWithImage:@"hyzx_qgxx" andTitle:@"帮助中心" andAction:@"helpCenter"],
                                                                                                          [[GYUserImageTitleCellModel alloc]initWithImage:@"hyzx_dhkf" andTitle:@"电话客服" andAction:@"callService"]
                                                                                                          ]];
        [details addObject:section1];
        [details addObject:section4];
        [details addObject:section6];
        _userDetails = details;
    
    return _userDetails;
}
#pragma mark - life
-(instancetype)init
{
    if (self = [super init]) {
        [[InterfaceManager shareInterface].specialInterfaces addObject:@"storageCheckAuth"];
        [[InterfaceManager shareInterface].specialInterfaces addObject:@"storageUserInfo"];
        
        [[InterfaceManager shareInterface].specialInterfaces addObject:@"LogisticsCheckAuth"];
        [[InterfaceManager shareInterface].specialInterfaces addObject:@"logisticsGetInfo"];
    }
    return self;
}
#pragma mark - method
+(instancetype)shareInterface
{
    static GYStorageUserViewModel *manager;
    kDISPATCH_ONCE_BLOCK(^{
        if (!manager) {
            manager = [GYStorageUserViewModel new];
        }
    });
    return manager;
}
-(void)requestCompanyAuthoInfo
{
    @WeakSelf;//
    NSString *interface =[[GYLoginViewModel shareInterface] roleType]==2? @"storageCheckAuth":@"LogisticsCheckAuth";
    [[InterfaceManager shareInterface]requetInterface:interface withParameter:nil handler:^(NSDictionary *info, InterfaceStatusModel *infoModel) {
        NSString *userCompanyAuthoStatus = [NSString stringWithFormat:@"%ld",infoModel.status];
        [selfp setUserCompanyAuthoStatus:userCompanyAuthoStatus];
    }];
}

-(void)requestUserShopBaseInfo
{
    NSInteger ltype=[[GYLoginViewModel shareInterface] roleType];
    @WeakSelf;
    NSString *interface = ltype==2?@"storageUserInfo":@"logisticsGetInfo";
    [[InterfaceManager shareInterface]requetInterface:interface withParameter:nil handler:^(NSDictionary *info, InterfaceStatusModel *infoModel) {
        NSDictionary *data = infoModel.data;
        if ([data isKindOfClass:[NSDictionary class]]) {
            
            GYStorageUserInfoModel *model = [GYStorageUserInfoModel yy_modelWithJSON:data];
            [selfp setStorageUserInfoModel:model];
            [self requestCompanyAuthoInfo];
        }
    }];
}

-(void)requestUpdateUserShopBaseInfo:(NSMutableDictionary *)para
{
  
    NSString *interface = [[GYLoginViewModel shareInterface] roleType]==2?@"storageChangeShopInfo":@"userLogisticsUpdateIntranduction";
    [[InterfaceManager shareInterface]requetInterface:interface withParameter:para handler:^(NSDictionary *info, InterfaceStatusModel *infoModel) {
        NSString *detail = para[@"introduction"] ?: @"";
        GYStorageUserInfoModel *model = [GYStorageUserViewModel shareInterface].storageUserInfoModel;
        model.introduction = detail;
        [[GYStorageUserViewModel shareInterface]setStorageUserInfoModel:model];
        [FTIndicator showSuccessWithMessage:@"保存成功"];
    }];
}

-(void)requestUpdateUserInfo:(NSMutableDictionary *)para;
{
    NSString *interface = [[GYLoginViewModel shareInterface] roleType]==2? @"storageChangeUserInfo":@"userLogisticsUpdateInfor";
    [[InterfaceManager shareInterface]requetInterface:interface withParameter:para handler:^(NSDictionary *info, InterfaceStatusModel *infoModel) {
        NSString *name = para[@"name"] ?: @"";
        NSString *qq = para[@"qq"] ?: @"";
        GYStorageUserModel *model = [GYStorageUserViewModel shareInterface].userModel;
        model.name = name;
        model.qq = qq;
        [[GYStorageUserViewModel shareInterface]setUserModel:model];
        [FTIndicator showSuccessWithMessage:@"保存成功"];
    }];
}

-(void)requestChangePassword:(NSMutableDictionary *)para
{
    NSString *interface = [[GYLoginViewModel shareInterface] roleType]==2?@"storageChangePsw":@"logisticsChangePsw";
    [[InterfaceManager shareInterface]requetInterface:interface withParameter:para handler:^(NSDictionary *info, InterfaceStatusModel *infoModel) {
        GYUserModel *model = [GYUserViewModel shareInterface].userModel;
        [[GYUserViewModel shareInterface]setUserModel:model];
        [[NSUserDefaults standardUserDefaults]setObject:para[@"newpwd"] forKey:kLoginPsw];
        [self setIsSuccess:YES];
        [FTIndicator showSuccessWithMessage:@"保存成功"];
    }];
}
-(void)requestModifyShopImage:(NSMutableDictionary *)para delete:(BOOL)isDelete end:(BOOL)isEnd
{
    
    NSString *interface = [[GYLoginViewModel shareInterface] roleType]==2?@"storageUpdateImage":@"userLogisticsUploadImage";
    if (isDelete)
    {
        interface = [[GYLoginViewModel shareInterface] roleType]==2?@"storageDeleteImage":@"userLogisticsDelImage";
    }
    [[InterfaceManager shareInterface]requetInterface:interface withParameter:para handler:^(NSDictionary *info, InterfaceStatusModel *infoModel) {
        [self setIsSuccess:YES];
        if (isEnd)
        {
             [FTIndicator showSuccessWithMessage:isDelete?@"删除成功":@"上传成功"];
        }
       
    }];
}

-(void)logout
{
    [self setStorageUserInfoModel:nil];
    [[GYCartViewModel shareInterface] requestDatas];
    [[InterfaceManager shareInterface].httpManager.requestSerializer setValue:nil forHTTPHeaderField:@"Authorization"];
}
@end
